package com.atguigu.Serivce.Impl;

import com.atguigu.Serivce.EmployeeService;
import com.atguigu.Utils.R;
import com.atguigu.mapper.EmployeeMapper;
import com.atguigu.pojo.Employee;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;

@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService{

    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 管理员登陆
     * @return
     */
    @Override
    public R login(HttpServletRequest request, Employee employee) {
        LambdaQueryWrapper<Employee> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee employee1 = employeeMapper.selectOne(queryWrapper);
        employee.setPassword(DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()));
        if(employee1==null){
            return R.error("账号错误");
        }else if (!employee1.getPassword().equals
                (employee.getPassword())){
            return R.error("密码错误");
        }else if (employee1.getStatus() == 0) {
            //0表示已被禁用
            return R.error("账号已被禁用");
        }
        request.getSession().setAttribute("employee",employee1.getId());
        employee1.setPassword(null);

        return R.success(employee1);
    }

    @Override
    public R logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @Override
    public R addEmployee(HttpServletRequest request, Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(new Date());
//        employee.setUpdateTime(new Date());
        employee.setStatus(1);
//        employee.setCreateUser((Long)request.getSession().getAttribute("employee"));
//        employee.setUpdateUser((Long)request.getSession().getAttribute("employee"));

        LambdaQueryWrapper<Employee> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeMapper.selectOne(queryWrapper);

        if (emp == null) {
            int rows = employeeMapper.insert(employee);
            return R.success("新增员工成功");
        }
        return R.error("该用户名已被注册");
    }

    @Override
    public R<Page<Employee>> page(Integer page, Integer pageSize, String name) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(name!=null,Employee::getUsername,name);
       queryWrapper.orderByAsc(Employee::getUpdateTime);
        employeeMapper.selectPage(pageInfo,queryWrapper);
        pageInfo.getRecords().forEach(System.out::println);
        return R.success(pageInfo);
    }

    @Override
    public R updateEmployee(HttpServletRequest request, Employee employee) {
//        Object attribute = request.getSession().getAttribute("employee");
//        employee.setUpdateUser((Long) attribute);
//        employee.setUpdateTime(new Date());
        employee.setStatus(employee.getStatus());

        log.info("当前线程Id："+Thread.currentThread().getId());

        employeeMapper.updateById(employee);

        return  R.success("修改成功") ;
    }

    @Override
    public R reCheck(HttpServletRequest request, Long id) {
        Employee employee = employeeMapper.selectById(id);

        return R.success(employee);
    }
}
